# CLI

Rsbuild comes with a lightweight CLI that includes commands such as [rsbuild dev](#rsbuild-dev) and [rsbuild build](#rsbuild-build).

## All commands

To view all available CLI commands, run the following command in the project directory:

```bash
npx rsbuild -h
```

The output is shown below:

```
Usage:
  $ rsbuild <command> [options]

Commands:
  dev      starting the dev server
  build    build the app for production
  preview  preview the production build locally
  inspect  inspect the Rspack and Rsbuild configs
```

## Common flags

Rsbuild CLI provides several common flags that can be used with all commands:

| Flag                       | Description                                                                                                                                     |
| -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `--base <base>`            | Specify the base path of the server, see [server.base](/config/server/base)                                                                     |
| `-c, --config <config>`    | Specify the configuration file, can be a relative or absolute path, see [Specify config file](/guide/configuration/rsbuild#specify-config-file) |
| `--config-loader <loader>` | Specify the config loader, can be `jiti` or `native`, see [Specify config loader](/guide/configuration/rsbuild#specify-config-loader)           |
| `--env-mode <mode>`        | Specify the env mode to load the `.env.[mode]` file, see [Env mode](/guide/advanced/env-vars#env-mode)                                          |
| `--env-dir <dir>`          | Specify the directory to load `.env` files, see [Env directory](/guide/advanced/env-vars#env-directory)                                         |
| `--environment <name>`     | Specify the name of environment to build, see [Build specified environment](/guide/advanced/environments#build-specified-environment)           |
| `-h, --help`               | Display help for command                                                                                                                        |
| `-m, --mode <mode>`        | Specify the build mode (`development`, `production` or `none`), see [mode](/config/mode)                                                        |
| `--no-env`                 | Disable loading `.env` files                                                                                                                    |
| `-r, --root <root>`        | Specify the project root directory                                                                                                              |

## rsbuild dev

The `rsbuild dev` command is used to start a local dev server and compile the source code for development.

```bash
Usage: rsbuild dev [options]

Options:
  -o, --open [url]      open the page in browser on startup
  --port <port>         specify a port number for Rsbuild server to listen
  --host <host>         specify the host that the Rsbuild server listens to
```

You can also start the dev server by running `rsbuild` directly, which is equivalent to running `rsbuild dev`.

```bash
npx rsbuild
```

### Opening page

The `--open` option allows you to automatically open a page when starting the dev server, which is equivalent to setting [server.open](/config/server/open) to `true`.

```bash
rsbuild dev --open
```

The `--open` option also supports specifying the URL to be opened, for example:

```bash
rsbuild dev --open http://localhost:3000/foo
```

The `--open` option can also be abbreviated to `-o`:

```bash
rsbuild dev -o
```

:::tip
When using [server.open](/config/server/open) and `--open` at the same time, `--open` takes precedence.
:::

## rsbuild build

The `rsbuild build` command will build the outputs for production in the `dist/` directory by default.

```bash
Usage: rsbuild build [options]

Options:
  -w --watch            turn on watch mode, watch for changes and rebuild
```

## rsbuild preview

The `rsbuild preview` command is used to preview the production build outputs locally. Note that you need to execute the `rsbuild build` command beforehand to generate the build outputs.

```bash
Usage: rsbuild preview [options]

Options:
  -o, --open [url]      open the page in browser on startup
  --port <port>         specify a port number for Rsbuild server to listen
  --host <host>         specify the host that the Rsbuild server listens to
```

:::tip
The preview command is only used for local preview. Do not use it for production servers, as it is not designed for that.
:::

## rsbuild inspect

The `rsbuild inspect` command is used to view the Rsbuild config and Rspack config of the project.

```bash
Usage: rsbuild inspect [options]

Options:
  --output <output>     specify the path to output in the dist (default: ".rsbuild")
  --verbose             show the full function in the result
```

When you run the command `npx rsbuild inspect` in the project root directory, the following files will be generated in the `dist/.rsbuild` directory of the project:

- `rsbuild.config.mjs`: Represents the Rsbuild configuration used during the build.
- `rspack.config.web.mjs`: Represents the Rspack configuration used during the build.

```bash
➜ npx rsbuild inspect

config inspection completed, generated files:

  - Rsbuild config: /project/dist/.rsbuild/rsbuild.config.mjs
  - Rspack config (web): /project/dist/.rsbuild/rspack.config.web.mjs
```

### Specifying mode

By default, the inspect command outputs the configuration for the development mode. You can add the `--env production` option to output the configuration for the production mode:

```bash
rsbuild inspect --mode production
```

### Verbose content

By default, the inspect command omits the content of functions in the configuration object. You can add the `--verbose` option to output the complete content of functions:

```bash
rsbuild inspect --verbose
```

### Multiple targets

If the current project has multiple build targets, such as building browser artifact and Node.js artifact simultaneously, multiple Rspack configuration files will be generated in the `dist/.rsbuild` directory.

```bash
➜ npx rsbuild inspect

config inspection completed, generated files:

  - Rsbuild config (web): /project/dist/.rsbuild/rsbuild.config.web.mjs
  - Rsbuild config (node): /project/dist/.rsbuild/rsbuild.config.node.mjs
  - Rspack config (web): /project/dist/.rsbuild/rspack.config.web.mjs
  - Rspack config (node): /project/dist/.rsbuild/rspack.config.node.mjs
```
